
var app = getApp()
Page({

  data: {
  	phone:'',
  	verification:'',
  	invitation:'',
    sendVer:'获取验证码',
    num:''
  },

  onLoad: function (options) {


  },
  //获取input内容
  getPhone(e){
  	this.setData({
  		phone:e.detail.value
  	})
  },
  getVer(e){
  	this.setData({
  		verification:e.detail.value
  	})
  },
  getInvitation(e){
  	this.setData({
  		invitation:e.detail.value
  	})
  },
  //注册
  register(){
  	if (this.data.phone=='') {
  		app.errorMsg('请输入手机号')
  	}else if(this.data.identifyCode==''){
      app.errorMsg('请输入验证码')
    }else{
      let data = {
       phone:this.data.phone,
       inviteCode:'',
       identifyCode:this.data.verification
     }
     console.log(data)
     app.request('wx_app/agent_account/add',data,'GET').then(res=>{
       console.log(res)
       if (res.code==1) {
          wx.setStorage({
          key:"memInfo",
          data:{
            agentId:res.data.agentId,
            id:res.data.id==null?0:res.data.id,
            gender:res.data.gender,
            username:res.data.username,
            name:res.data.name,
            role:res.data.role,
            isEnable:res.data.isEnable,
            shop:res.data.agentCompanyName,
            isVisitor:res.data.isVisitor,
            vipUserName:res.data.vipUserName,
            phone:res.data.phone,
            vipName:res.data.vipName,
            vipId:res.data.vipId
          }
        })
        wx.login({
          success: function(data) {
            console.log(data)
            if (data.code) {
              let code = {
                code:data.code,
                agentAccountId:res.data.id
            }
              console.log(code)
              app.request('wx_app/getOpenId',code,'GET').then(item=>{
                console.log(item)
              })
            } else {

            }
          }
        });
        wx.navigateTo({
          url:'setPassword?regAccountId='+res.data.id
        })
      }else{
        app.errorMsg(res.message)
      }
    })
   }
 },
  //发送验证码
  sendVer(){
    if (this.data.sendVer==='获取验证码') {
      if (this.data.phone=='') {
        app.errorMsg('请输入手机号')
      }else{
        let data = {
          phoneNum:this.data.phone
        }
        console.log(data)
        app.request('wx_app/sendSmsCode',data,'GET').then(res=>{
          console.log(res)
          if (res.code==0) {
            app.errorMsg(res.message)
          }
        })
        this.setData({
          num:120,
          sendVer:'120s后重新发送'
        })
        let time = setInterval(()=>{
          let num = this.data.num
          num --
          this.setData({
            num,
            sendVer:num+'s后重新发送'
          })

          if(num == 0){
           clearInterval(time)
           this.setData({
            sendVer:'获取验证码'
          })
         }
       },1000)
      }
    }else{

    }
  }
})